<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

require_once 'BluesCoders/View/Helper/Event.php';
require_once 'BluesCoders/View/Event.php';

/**
 * Basic view implementation, use PHP itself as template engine.
 *
 * @author miho
 */
class BluesCoders_View_Standart {

    protected $_scriptPath;

    protected $_renderResult;

    protected $_params = array();

    public function setScriptPath($value) {
        $this->_scriptPath = $value;
        return $this;
    }

    public function render() {
        if( !file_exists($this->_scriptPath)) {
            throw new Exception('Path '.$this->_scriptPath .' is invalid');
        }
        BluesCoders_Event_Dispatcher::getIsntance()->dispatchEvent(
            new BluesCoders_View_Event(BluesCoders_View_Event::PRE_RENDER, $this)
        );

        ob_start();
        include $this->_scriptPath;
        $this->_renderResult = ob_get_clean();

        $postDispatchEvent = new BluesCoders_View_Event(BluesCoders_View_Event::POST_RENDER, $this, null);

        BluesCoders_Event_Dispatcher::getIsntance()->dispatchEvent(
            new BluesCoders_View_Event(BluesCoders_View_Event::POST_RENDER, $this, $this->_renderResult)
        );
        return $this->_renderResult;
    }

    public function  __call($name,  $arguments) {
        $event = new BluesCoders_View_Helper_Event(
            BluesCoders_View_Helper_Event::CALL,
            strtolower($name),
            $this,
            $arguments
        );
        if(BluesCoders_Event_Dispatcher::getIsntance()->dispatchEvent($event) === FALSE){
            throw new Exception('Call undefined method '.$name);
        }

        return $event->getResult();
    }

    public function  __get($name) {
        return $this->getParam($name);
    }

    public function getParam($name) {
        return isset($this->_params[$name]) ? $this->_params[$name] : null;
    }

    public function setParam($name, $value) {
        $this->_params[$name] = $value;
    }

    public function addParams($params) {
        $this->_params = array_merge($this->_params, $params);
        return $this;
    }
}
?>
